package com.yzf.demo.demo.io;

import java.io.*;

/**
 * Created by 于占峰 on 2020/4/5/005.
 * 字符转换
 */
public class ConvertDemo {
    public static void main(String[] args) {
        //InputStreamReader
        //OutputStream
        try {
            //字符输入流

            //普通的inputStream
            InputStream inputStream = new FileInputStream("G:/HellWorld.txt");
            //包装的BufferedInputStream
            BufferedInputStream bufferedInputStream =
                    new BufferedInputStream(new FileInputStream("G:/HellWorld.txt"));
            //可以传入要转化的字符集 不传入则使用平台默认的字符集
            InputStreamReader inputStreamReader =
                    new InputStreamReader(inputStream, "UTF-8");
            //包装的bufferReader 传入 inputStreamReader
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            //bufferedReader可以读取一列
            System.out.println(bufferedReader.readLine());


            //字符输出流

            OutputStreamWriter outputStreamWriter =
                    new OutputStreamWriter(new FileOutputStream("G:/123.txt"), "UTF-8");
            BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
            bufferedWriter.write("哈哈哈哈");
            bufferedWriter.flush();//要刷新缓存 不然会没有数据
//            int len = 0;
//            char[] chars = new char[1024];
//            while ((len = inputStreamReader.read())!= -1){
//                System.out.println(new String(chars,0,len));
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
